﻿using System;
using System.Windows;
using System.Windows.Media;

namespace VisualView.Axis
{
    /// <summary>
    /// 线性轴
    /// </summary>
    public class LinearNumberAxis : BaseNumberAxis
    {
        public override double ValueToAxisValue(double value, object param)
        {
            return value;
        }

        public override double AxisValueToValue(double value, object param)
        {
            return value;
        }

        public override void Refresh()
        {
            var length = GetLength();
            if (length == 0d) return;
            var param = GetValueLocationParam() as NumberAxisValueLocationConvertParam;
            GetAxisStartAndPace(param, out double start, out double pace);
            var count = param.AxisValueDifference / pace;
            using (var dc = Visual.RenderOpen())
            {
                dc.DrawRectangle(Background, null, new Rect(0, 0, this.ActualWidth, this.ActualHeight));
                DrawingAxisLine(dc);
                for (int i = -1; i < count; i++)
                {
                    var axisValue = start + i * pace;
                    var location = GetAxisValueLocation(axisValue, param);
                    if (location >= 0 && location <= param.Length)
                    {
                        var v = AxisValueToValue(axisValue);
                        var vstr = (v >= Convert.ToDouble(decimal.MaxValue) || v <= Convert.ToDouble(decimal.MinValue)) ? v.ToString() : Convert.ToDecimal(v).ToString();
                        var sg = CreateTextGeometry(vstr, FontSize, out double width, out double height);
                        if (param.AxisType == AxisType.X)
                        {
                            dc.DrawLine(LinePen, new Point(location, 0), new Point(location, 5));
                            sg.Transform = new MatrixTransform(1, 0, 0, 1, location - width / 2, 5);
                        }
                        else if (param.AxisType == AxisType.X2)
                        {
                            dc.DrawLine(LinePen, new Point(location, this.ActualHeight), new Point(location, this.ActualHeight - 5));
                            sg.Transform = new MatrixTransform(1, 0, 0, 1, location - width / 2, this.ActualHeight - height - 5);
                        }
                        else if (param.AxisType == AxisType.Y)
                        {
                            dc.DrawLine(LinePen, new Point(this.ActualWidth, location), new Point(this.ActualWidth - 5, location));
                            sg.Transform = new MatrixTransform(0, -1, 1, 0, this.ActualWidth - 5 - height, location + width / 2);
                        }
                        else
                        {
                            dc.DrawLine(LinePen, new Point(0, location), new Point(5, location));
                            sg.Transform = new MatrixTransform(0, 1, -1, 0, 5 + height, location - width / 2);
                        }
                        dc.DrawGeometry(TextPen.Brush, TextPen, sg);
                    }
                }
            }
        }
    }
}
